package com.itheima;

import com.itheima.domain.Student;
import com.itheima.mapper.StudentMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class StudentServiceTest {

    private SqlSession sqlSession;
    private StudentMapper dao;

    @Before
    public void init() throws IOException {
        //1. 加载核心配置文件
        InputStream in = Resources.getResourceAsStream("MyBatisConfig.xml");

        //2. 获取SqlSessionFactoryBuilder
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();

        //3. 获取SqlSessionFactory
        SqlSessionFactory factory = builder.build(in);

        sqlSession = factory.openSession(true);

        // 创建Dao代理对象
        dao = sqlSession.getMapper(StudentMapper.class);

    }

    @After
    public void destroy() {
        sqlSession.close();
    }

    // 分页查询
    @Test
    public void findAll() {
        List<Student> students = dao.findAll();
        for (Student student : students) {
            System.out.println(student);
        }
    }
}
